/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.monitoring.admin;

import com.x3.monitoring.ApplicationContext;
import com.x3.monitoring.dao.PerusahaanDAO;
import com.x3.monitoring.dao.mysql.PerusahaanDAOImpl;
import com.x3.monitoring.entity.Perusahaan;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Space;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

/**
 *
 * @author Hendro Steven
 */
public class CariJumlahTenagaKerjaPerusahaanWnd extends ApplicationContext {

    Checkbox chkTKI;
    Checkbox chkTKA;
    Intbox intLkTKI;
    Intbox intPrTKI;
    Intbox intLkTKA;
    Intbox intPrTKA;
    Listbox lstHasil;

    public CariJumlahTenagaKerjaPerusahaanWnd() {
    }

    public void onCreate() throws Exception {
        chkTKI = (Checkbox) getFellow("chkTKI");
        chkTKA = (Checkbox) getFellow("chkTKA");
        intLkTKI = (Intbox) getFellow("intLkTKI");
        intPrTKI = (Intbox) getFellow("intPrTKI");
        intLkTKA = (Intbox) getFellow("intLkTKA");
        intPrTKA = (Intbox) getFellow("intPrTKA");
        lstHasil = (Listbox) getFellow("lstHasil");
    }

    private String getSQL() {
        List<String> kondisi = new ArrayList<String>();
        String sql = "SELECT * FROM perusahaan";
        if (chkTKI.isChecked()) {
            kondisi.add("(jml_tki_l=" + intLkTKI.getValue() + " OR jml_tki_p=" + intPrTKI.getValue() + ")");
        }
        if (chkTKA.isChecked()) {
            kondisi.add("(jml_tka_l=" + intLkTKA.getValue() + " OR jml_tka_p=" + intPrTKA.getValue() + ")");
        }
        if (kondisi.size() > 0) {
            sql = sql + " WHERE ";
            for (int x = 0; x < kondisi.size(); x++) {
                sql += kondisi.get(x);
                if (x < kondisi.size() - 1) {
                    sql += " AND ";
                }
            }
        }
        System.out.println(sql);
        return sql;
    }

     public void cari() throws Exception {
        Connection conn = getConn();
        try {
            //System.out.println(sql);
            PerusahaanDAO dao = new PerusahaanDAOImpl(conn);
            List<Perusahaan> list = dao.getsPerusahaan(getSQL());
            lstHasil.getItems().clear();
            int no = 1;
            for (final Perusahaan p : list) {
                Listitem item = new Listitem();
                item.setValue(p);
                item.appendChild(new Listcell(p.getNamaPerusahaan()));
                item.appendChild(new Listcell(p.getNamaPimpinan()));
                item.appendChild(new Listcell(p.getAlamatJalan() + ", " + p.getKota() + " Telp: " + p.getTelp() + " Fax: " + p.getFax()));
                item.appendChild(new Listcell("L = "+p.getJmlTKILaki()+", P = "+p.getJmlTKIPerem()));
                item.appendChild(new Listcell("L = "+p.getJmlTKALaki()+", P = "+p.getJmlTKAPerem()));
                Toolbarbutton btnDetail = new Toolbarbutton();
                btnDetail.setId("btnDetail" + (no++));
                btnDetail.setImage("/img/detail.png");
                btnDetail.setTooltiptext("Klik di sini untuk Detail dan Perubahan data");
                btnDetail.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        //panggil form edit
                        Window win = (Window) Executions.createComponents("/zul/admin/editPerusahaan.zul", null, null);
                        Textbox txtIdPerusahaan = (Textbox) win.getFellow("txtIdPerusahaan");
                        txtIdPerusahaan.setValue(p.getId());
                        win.doModal();
                        cari();
                    }
                });

                Toolbarbutton btnHapus = new Toolbarbutton();
                btnHapus.setId("btnHapus" + no++);
                btnHapus.setImage("/img/delete.png");
                btnHapus.setTooltiptext("Klik untuk mengahapus Perusahaan");
                btnHapus.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        if (Messagebox.show("Hapus?", "Konfirmasi", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION) == Messagebox.YES) {
                            Connection connDel = getConn();
                            try {
                                PerusahaanDAO daoDel = new PerusahaanDAOImpl(connDel);
                                daoDel.delete(p.getId());
                                cari();
                            } catch (Exception ex) {
                                Messagebox.show(ex.getMessage());
                            } finally {
                                connDel.close();
                            }
                        }
                    }
                });

                Listcell cellAksi = new Listcell();
                cellAksi.appendChild(btnDetail);
                cellAksi.appendChild(new Space());
                cellAksi.appendChild(btnHapus);
                item.appendChild(cellAksi);
                lstHasil.appendChild(item);
            }
        } catch (Exception ex) {
            Messagebox.show(ex.getMessage());
        } finally {
            conn.close();
        }
    }

    public void cetak() throws Exception {
        Window win = (Window) Executions.createComponents("/zul/admin/rpt_daftar_perusahaan.zul", null, null);
        Textbox sql = (Textbox) win.getFellow("sql");
        sql.setValue(getSQL());
        win.doModal();
    }
}
